<?php

namespace App\Services\Index;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\Settlement\Info as SettlementInfo;
use App\Models\Game\Stats\User\Day;
use App\Models\Game\User\User;
use App\Core\Traits\Numeral;
use Helper\Timed;

class SummaryService extends Service
{
    use DatabaseMysql, Numeral;

	/**
	 * 概要统计【注册人数】
	 */
	public function playerRegister($request)
	{
		$query = User::dbTable();
		// 筛选【平台】
		$this->makeWhereOrg($request, $query);

		$querySum = (clone $query);
		// 筛选【时间】
		$this->makeWhereParamDatetimeTimeZone($request, $querySum, 'registration_time', 'datetime',0, 0, 'default', false);
		// 数据合计
		$total = $querySum->count();

		// 筛选【时间】最近7天
		$this->makeWhereLastDays($request, $query, 'registration_time', 7);
		// 统计数据【数据列表】
		//转换指定时区
		date_default_timezone_set(Timed::getTimezoneName());
		$collection = $query->selectRaw(implode(',', [
			"DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(registration_time), '%Y-%m-%d %H:%i:%s'), '+08:00', '".(preg_replace("/^UTC/", "",
				Timed::getTimeCode()))."')) as timed",
			"COUNT(*) AS amount"
		]))
			->groupBy('timed')
			->get();

		$collection->transform(function ($item) {
			$item['timed'] = strtotime($item['timed']);
			return $item;
		});

		return [
			$collection,
			$total
		];
	}

	/**
	 * 概要统计【游戏人数】
	 */
	public function playerGame($request)
	{
		$query = SettlementInfo::dbTable();
		// 筛选【平台】
		$this->makeWhereOrg($request, $query);
		// 筛选【类型】
		$this->makeWhere($query, 'player_type', SettlementInfo::PLAYER_TYPE);

		$querySum = (clone $query);
		// 筛选【时间】
		$this->makeWhereParamDatetimeTimeZone($request, $querySum, 'end_time');
		// 数据合计
		$total = $querySum->distinct('uid')->count();

		// 筛选【时间】最近7天
		$this->makeWhereLastDays($request, $query, 'end_time', 7);
		// 统计数据【数据列表】
		date_default_timezone_set(Timed::getTimezoneName());
		$collection = $query->selectRaw(implode(',', [
			"DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(end_time), '%Y-%m-%d'), '+08:00', '".(preg_replace("/^UTC/", "",
				Timed::getTimeCode()))."')) AS timed",
			"COUNT(DISTINCT uid) AS amount"
		]))
			->groupBy('timed')
			->get();

		//转换指定时区
		$collection->transform(function ($item) {
			$item['timed'] = strtotime($item['timed']);
			return $item;
		});

		return [
			$collection,
			$total
		];
	}

	/**
	 * 概要统计【新增游戏人数】
	 */
	public function playerGameNew($request)
	{
		$query = User::dbTable();
		// 筛选【平台】
		$this->makeWhereOrg($request, $query);

		$querySum = (clone $query);
		// 筛选【时间】
		$this->makeWhereParamDatetimeTimeZone($request, $querySum, 'first_game_time', 'datetime',0, 0, 'default', false);
		// 数据合计
		$total = $querySum->count();

		// 筛选【时间】最近7天
		$this->makeWhereLastDays($request, $query, 'first_game_time', 7);
		// 统计数据【数据列表】
		date_default_timezone_set(Timed::getTimezoneName());
		$collection = $query->selectRaw(implode(',', [
			"DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(first_game_time), '%Y-%m-%d %H:%i:%s'), '+08:00', '".(preg_replace("/^UTC/", "",
				Timed::getTimeCode()))."')) AS timed",
			"COUNT(*) AS amount"
		]))
			->groupBy('timed')
			->get();

		//转换指定时区
		$collection->transform(function ($item) {
			$item['timed'] = strtotime($item['timed']);
			return $item;
		});

		return [
			$collection,
			$total
		];
	}

	/**
	 * 概要统计【平台收益】
	 */
	public function amount($request)
	{
		$query = SettlementInfo::dbTable();
		// 筛选【类型】
		$this->makeWhere($query, 'player_type', SettlementInfo::PLAYER_TYPE);
		// 筛选【平台】
		$this->makeWhereOrg($request, $query);

		$querySum = (clone $query);
		// 筛选【时间】
		$this->makeWhereParamDatetimeTimeZone($request, $querySum, 'end_time');
		// 数据合计
		$total = -$querySum->sum('usd->gain_player');

		// 筛选【时间】最近7天
		$this->makeWhereLastDays($request, $query, 'end_time', 7);
		// 统计数据【数据列表】
		date_default_timezone_set(Timed::getTimezoneName());
		$collection = $query->selectRaw(implode(',', [
			"DATE(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(end_time), '%Y-%m-%d'), '+08:00', '".(preg_replace("/^UTC/", "",
				Timed::getTimeCode()))."')) AS timed",
			"CAST(-SUM(usd->'$.gain_player') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount"
		]))
			->groupBy('timed')
			->get();

		//转换指定时区
		$collection->transform(function ($item) {
			$item['timed'] = strtotime($item['timed']);
			return $item;
		});

		return [
			$collection,
			$total
		];
	}
}
